home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / miracle.zip / EXAMPLE.C < prev    next >
C/C++ Source or Header  |  1993-02-08  |  13KB  |  581 lines

  1. /*
  2.    Demonstrates many of the capabilities of C compiler
  3.  
  4.    typedefs, structs, function pointers, nested structs, initializers,
  5.    enums, unions, local static variables etc
  6. */
  7.  
  8. union
  9.    {
  10.    int x;
  11.  
  12.    struct astrtype
  13.       {
  14.       int x1 : 13, x2 : 2;
  15.       int x3 : 7,  x4 : 6;
  16.  
  17.       int b;
  18.       }
  19.       y, *yp;
  20.    } uni, *unip;
  21.  
  22. union { int a; char b; } seven = {7};
  23.  
  24. struct astrtypeo { int a; char *s; int b; } astr = { 1, "astr", 2 };
  25.  
  26. enum { red, blue } colour = blue;
  27.  
  28. double doud=2.3232;
  29. long woper=765432L;
  30.  
  31. int m1[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } },
  32.  
  33.     m2[2][3][2] = { { {1,2},{3,4},{5,6} },
  34.             { {7,8},{9,1},{2,3} } },
  35.     m3[3] = { 1,2,3 };
  36.  
  37. char ra[]="this is a string", *rb=&((ra+2)[3]);
  38.  
  39. struct
  40.    {
  41.    int x1 : 13, x2 : 2;
  42.    int x3 : 7,  x4 : 6;
  43.  
  44.    int b;
  45.    }
  46.    *azyp, azy = { 1, 2, 3, 4, 5 };
  47.  
  48. char uu[5][4][3]; char uu[][4][3];
  49. int yyt=4?2:1+7;
  50.  
  51. char tt[][10]={"this","is","a","word"},
  52.      day1[][10]={"sunday","monday","et","cetera","etc"},
  53.      ff[]="another string", *day2[]={"tuesday","wednesday"};
  54.  
  55. int zza[] = { 1, 2, 3 }, zzb[] = { 4, 5, 6 }, zzc[] = {7, 8, 9},
  56.     aa[] = { 32 }, qq=96+1, *hh=&qq,
  57.     *zzpa[] = { zza, zzb, zzc },
  58.     **qqp[] = { zzpa, zzpa+1, &(zzpa[1]) };
  59.  
  60. typedef void vfun();
  61. vfun main,arsub,filefunc,scopefun;
  62. void printnum(int n), subrun(int a,int b);
  63. int  subbi(), n, subtwo(int a,int b);
  64. long lfun();
  65.  
  66. int  tax[] = { 1, 2, 'a' }, tox[5] = { 4, 5, 'b', 6, 7 };
  67. char cax[] = { 1, 2, 'a' }, cox[5] = { 4, 5, 'b', 6, 7 };
  68.  
  69. unsigned zw=0xface;
  70. char zm[]="abc", zn[]={'a','b','c','\0'};
  71.  
  72. struct mak { int a; char b; } yone, *ytwo, globar[5];
  73. struct sec { long alpha, *beta[4], *gamma; char b; int *x, y, z[2]; } *s1;
  74.  
  75. typedef int alphat, betat[5], gammat;
  76. alphat alfa;
  77. betat betertee;
  78.  
  79. struct mak macone, *mactwo, macthree[3];
  80.  
  81. typedef int *upperbongo;
  82.  
  83. typedef int *FPTRA();
  84.  
  85. typedef int (*FPTR)();
  86.  
  87. union mate { int a; char b; long *c; char d; };
  88. int ;
  89.  
  90. struct mystruct
  91.    {
  92.    struct mak myitem;
  93.    long longvar;
  94.    int intvar;
  95.    };
  96.  
  97. #define suminus(j,a) j+a-2
  98.  
  99. char yyz[]="yyzb", *cat="whiskas", ssbuf[20]="hello world";
  100.  
  101. int  *giarp, arri[10], *giary[5];
  102. long *glurp, arrl[5], *blurb[10];
  103.  
  104. char cvary='a';
  105. int ivar=42, gvar=45;
  106.  
  107.  
  108. #include <io.h>
  109. #include <stdio.h>
  110. #include <string.h>
  111. #include <system.h>
  112.  
  113. int asub(z,x,y) char z; int x,y; { printf("%c",z); return x+y; }
  114.  
  115. void main()
  116. {
  117.    int dgvar=gvar, gvar=7;
  118.  
  119.    void *varptr, *varray[10];
  120.    int    (*funpt)();
  121.    FPTR funptrr;
  122.    void (*subp)(int p1, int p2);
  123.    int    (*subq)(int p1, int p2);
  124.  
  125.    double xx=3.141592, yy=1.234567;
  126.  
  127.    static int stak1, *stak2[20];
  128.    static struct mak makk[2], *makk2, *makk3, *makk4;
  129.  
  130.    int    sum, value, test, iar[10], *iarray[10];
  131.    unsigned char flag, *apt, car[5];
  132.    long *lap, lar[4], lvar1, lvar2;
  133.  
  134.    struct mak indie, *indip, *idip[3], idi[4], indthree;
  135.    struct mystruct anstruct;
  136.  
  137.    enum numer { zero, one, two, free } numba;
  138.  
  139.    typedef struct astag { int x,y; struct astag *z; } astype;
  140.    astype asinst;
  141.  
  142.    struct pal
  143.       {
  144.       struct mal
  145.      {
  146.      int a;
  147.      char b;
  148.      }
  149.      palne;
  150.  
  151.       struct
  152.      {
  153.      int w1, w2;
  154.      long lw;
  155.      }
  156.      mfing;
  157.  
  158.       union mate matey, *pmate;
  159.       long *longvar;
  160.       int intvar;
  161.       };
  162.  
  163.    static int svar=24;
  164.    static char sch='b';
  165.  
  166.    double ddza,ddzb;
  167.  
  168.    union { 
  169.     struct { unsigned int l1; unsigned int l2;
  170.           unsigned int l3; unsigned int l4; } lx;
  171.  
  172.     double b; }
  173.         ab;
  174.  
  175.    static double pipi=3.1415926536;
  176.    double duba=1.2345-1.1111, dubb=duba*1.1111;
  177.    int mrakx,mraky;
  178.  
  179.    long wopa=234567L;
  180.    static long wopra=134567L;
  181.  
  182.    int mama=34,Mama=43,Ma_ma=42;
  183.  
  184.    int mack=5;
  185.  
  186.    struct pal palthing;
  187.    struct mal mal1[2];
  188.  
  189.    struct astruct
  190.       {
  191.       char name[10];
  192.       int x,y;
  193.       }
  194.       namei[10] = { { "Myname", 4, 5 },
  195.             { "2name",    2, 3 } };
  196.  
  197.    struct bstruct
  198.       {
  199.       char x[10];
  200.       struct astruct abstruct;
  201.       }
  202.       acount[10] = { { "first",     { "Myname",   9, 5 } },
  203.                  { "second", { "Yourname", 2, 7 } } };
  204.  
  205.    struct { int t[3][2][4]; long h; } ilstruct[2][4], *ilsptr;
  206.  
  207.    int (*fptr)(char *fmt,...), i,j,k;
  208.    int uu[9][3];
  209.    char dstr[5]="aabb";
  210.  
  211.    int *xt=uu[2];
  212.    int sival=subbi();
  213.  
  214.    int ux[3][4]={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
  215.    int uy[3][4]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  216.  
  217.    char *day[]     = { "Mon", "Tues", "Wed" };
  218.    char s[5][10] = { "cat", "dog", "bird" };
  219.  
  220.    static int qa[]={1,2,3}, qb[]={4,5,6}, qc[]={7,8,9};
  221.    static int *pa[]={qa,qb,qc};
  222.  
  223. struct
  224.    {
  225.    int x1 : 13, x2 : 2;
  226.    int x3 : 7,  x4 : 6;
  227.  
  228.    int b;
  229.    }
  230.    *zyp, zy = { 1, 2, 3, 4, 5 };
  231.  
  232.    printf("%d ",asub('x',1,2));
  233.    printf("%d%d%d%d%d",zy.x1,zy.x2,zy.x3,zy.x4,zy.b);
  234.    printf("%d%d%d%d%d ",azy.x1,azy.x2,azy.x3,azy.x4,azy.b);
  235.  
  236.    uni.x=6; printf("%d ",uni.y.x2);
  237.    uni.y.x1=5; uni.y.x2=1; uni.y.x3=23; uni.y.x4=11;
  238.  
  239.    printf("%d %d %d %d ",uni.y.x1,uni.y.x2,uni.y.x3,uni.y.x4);
  240.    printf("%d %d ",uni.y.x1+uni.y.x2,uni.y.x3+uni.y.x4);
  241.    printf("%d %d ",uni.y.x1-uni.y.x2,uni.y.x3-uni.y.x4);
  242.  
  243.    printf("%d %d ",uni.y.x1*uni.y.x2,uni.y.x3*uni.y.x4);
  244.    printf("%d %d ",uni.y.x1/uni.y.x2,uni.y.x3/uni.y.x4);
  245.  
  246.    uni.y.x1+=7; printf("%d ",uni.y.x1);
  247.    uni.y.x1*=2; printf("%d ",uni.y.x1);
  248.    uni.y.x1/=3; printf("%d ",uni.y.x1);
  249.    uni.y.x1%=3; printf("%d ",uni.y.x1);
  250.  
  251.    printf("%d ",++uni.y.x2); uni.y.x2++; printf("%d ",uni.y.x2++);
  252.    printf("%d ",uni.y.x2); ++uni.y.x2; printf("%d ",uni.y.x2);
  253.    printf("%d ",++uni.y.x2);
  254.  
  255.    zy.x3=28; zyp=&zy;
  256.    printf("-> %d %d\n",zy.x3,zyp->x3);
  257.  
  258.    for(i=0; i<3; i++)
  259.       for(j=0; j<4; j++)
  260.      printf("%d%d",ux[i][j],uy[i][j]);
  261.  
  262.    for(i=0; i<3; i++)
  263.       for(j=0; j<3; j++)
  264.      printf("%d",pa[i][j]);
  265.    printf("%d %d ",dgvar,gvar);
  266.  
  267.    scopefun();
  268.  
  269.    printf("%d%d%d%d%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",
  270.       sizeof(astr),sizeof(m1),sizeof(m2),sizeof(m3),sizeof(ra),sizeof(rb),
  271.       sizeof(tt),sizeof(day1),sizeof(ff),sizeof(day2),
  272.       sizeof(zza),sizeof(zzb),sizeof(zzc),sizeof(qq),sizeof(aa),sizeof(hh),
  273.       sizeof(zzpa),sizeof(qqp),sizeof(tax));
  274.  
  275.    printf("%d %d %d %d %d %d %d %d %d %d %d %d\n",
  276.       sizeof(tox),sizeof(cax),sizeof(cox),sizeof(zw),
  277.       sizeof(zm),sizeof(zn),sizeof(yyz),sizeof(cat),
  278.       sizeof(ssbuf),sizeof(cvary),sizeof(ivar),seven.a);
  279.  
  280.    printf("%c%d %d%d%s %d %d ",*rb,sival,astr.a,astr.b,astr.s,
  281.         sizeof(m2[1]),sizeof(tt[1]));
  282.    for(i=0; i<4; i++) printf("%s ",tt[i]);
  283.  
  284.    for(i=0; i<2; i++)
  285.       for(j=0; j<3; j++)
  286.      for(k=0; k<2; k++)
  287.         printf("%d ",m2[i][j][k]);
  288.    printf("\n");
  289.  
  290.    ilstruct[1][3].t[1][1][2]=4; ilsptr=&ilstruct[1][3];
  291.    printf("%d%s%d ",ilsptr->t[1][1][2],dstr,*hh);
  292.  
  293.    printf("%d %d %d %d %d %d %d %d %d %d %d %d X ",zza[0],zza[1],zza[2],
  294.       sizeof(char),sizeof(char),*aa,zzb[0],zzb[1],zzb[2],zzc[0],zzc[1],zzc[2]);
  295.  
  296.    for(i=0; i<3; i++)
  297.       for(j=0; j<2; j++)
  298.      for(k=0; k<3; k++)
  299.         printf("%d ",qqp[i][j][k]);
  300.  
  301.    printf("\n%d %d %d %s %s %s mack=%d 5  ivar=%d 42 cvary=%x a(%x) ",
  302.     tax[0],tax[1],tax[2],ssbuf,yyz,cat,mack,ivar,cvary,'a');
  303.  
  304.    printf("%d%d%d%d",yy<xx,xx<yy,yy==xx,xx!=yy);
  305.    printf("%d%d%d%d",yy<=xx,xx<=yy,yy>xx,xx>yy);
  306.    printf("%d%d\n",yy>=xx,xx>=yy);
  307.  
  308.    printf("%d %x tox %d %d %d %d %d sv=%d 24 sc=%x b(%x)\n",
  309.       zw,zw,tox[0],tox[1],tox[2],tox[3],tox[4],svar,sch,'b');
  310.  
  311.    cat=ssbuf;
  312.    printf("%s%s %s cax %d %d %d cox %d %d %d %d %d\n",zm,zn,
  313.       cat,cax[0],cax[1],cax[2],cox[0],cox[1],cox[2],cox[3],cox[4]);
  314.  
  315.    (fptr=&printf,*fptr)("hello");
  316.  
  317.    filefunc();
  318.  
  319.    funptrr=&subbi; printf("%d ",(*funptrr)());
  320.    subrun(12,3+subtwo(4,1+subtwo(3,4))); printf("\n");
  321.  
  322.    subp=&subrun; (*subp)(12,3+subtwo(4,7));
  323.    subq=&subtwo; (*subp)(12,3+(*subq)(4,7));
  324.  
  325.    betertee[3]=2; alfa=betertee[3]; makk4=1+makk;
  326.    makk[1].a=37; makk2=&makk[1]; makk3=makk+1; stak1=3; stak2[0]=&stak1;
  327.    printf("%d%d %d=%d=%d ",stak1,*stak2[0],makk2->a,makk3->a,makk4->a);
  328.  
  329.    printf("%d %d %d ",sizeof(struct pal),sizeof(numba),sizeof(palthing));
  330.    printf("%d %d %d ",sizeof(struct pal *), sizeof(void *), sizeof(mal1));
  331.    printf("%d %d%d\n",sizeof(mal1[1]),sizeof(&mal1[1]),alfa);
  332.  
  333.    arsub(); printf("%ld ",lfun());
  334.  
  335. somewere:
  336.  
  337.    palthing.palne.b=65;
  338.    anstruct.myitem.b=65; indip=&(anstruct.myitem);
  339.    palthing.mfing.w2=89; mal1[1].b=65;
  340.    printf("%d %d",palthing.mfing.w2,palthing.palne.b);
  341.    printf("%c%c%c",mal1[1].b,indip->b,"tunes"[1]);
  342.  
  343.    if(stak1==3) { stak1=4; goto somewere; }
  344.  
  345.    indie.a=4; globar[2].b='y'; idip[2]=&idi[2]; idi[2].a=42;
  346.    printf("%d %c %d ",indie.a,globar[2].b,idi[2].a);
  347.  
  348.    indip=&idi[2]; indip->a=43;
  349.    printf("%d %c %d %d ",indie.a,globar[2].b,idi[2].a,indip->a);
  350.  
  351.    for(i=0; i<3; i++) printf("%s ",s[i]);
  352.    for(i=0; i<3; i++) printf("%s ",day[i]);
  353.    printf("\n");
  354.  
  355.    sum=suminus(1,2+3);
  356.    do printf("%d ",sum--); while(sum);
  357.  
  358.    numba=free; printf("numba=%d ",numba);
  359.  
  360.    printnum(2*3+7);
  361.    sum=3; sum+=5; printnum(sum); sum<<=1; printnum(sum);
  362.    sum>>=2; printnum(sum); sum%=3; printnum(sum);
  363.    putchar((2>1)?'y':'n'); putchar((2>3)?'n':'y');
  364.  
  365.    /* local stack and heap arrays */
  366.  
  367.    iarray[9]=&iar[4]; *iarray[9]=7;
  368.    apt=&car[2]; *apt=1; iar[0]=3; *iar=2;
  369.  
  370.    lvar1=(long)42; lvar2=lvar1+(long)12;
  371.    varray[3]=varptr; iar[5]=1; apt=malloc(20); apt[3]='b'; car[0]='c';
  372.    lap=malloc(20); lap[3]=2; lar[2]=3;
  373.  
  374.    printf("%d%d%d%d %ld%ld ",iar[0],*iar,iar[4],car[2],lap[3],lar[2]);
  375.    printf("%d",iar[5]); printf("%c%c%ld ",apt[3],car[0],lvar2);
  376.  
  377.    /* global static and heap arrays */
  378.  
  379.    giary[2]=&iar[4]; *giary[2]=6; printf("%d",iar[4]);
  380.    cat=malloc(10); cat[3]='d'; printf("%c",cat[3]);
  381.    giarp=malloc(10); giarp[2]=4; arri[2]=5;
  382.    printf("%d%d",giarp[2],arri[2]); glurp=malloc(20); glurp[0]=6;
  383.    arrl[1]=7; printf("%ld%ld\t",glurp[0],arrl[1]);
  384.  
  385.    subrun(12,3+subtwo(4,7));
  386.    printf("\n");
  387.  
  388.    if((1>2) && 3) printf(" no");
  389.    if((2>1) && 3) printf(" yes");
  390.  
  391.    if(1>2) printf("n"); else printf("y");
  392.    if(2>1) printf("y"); else printf("n");
  393.  
  394.    test=3*4+1; printf("%d",test);
  395.  
  396.    for(sum=0, value=1; value<11; value=value+1) sum=sum+value;
  397.    printf("sum to ten %d  ",sum);
  398.  
  399.    for(value=1; value<=4; value++)
  400.       switch(value)
  401.      {
  402.      case 1:  printf("one ");
  403.           break;
  404.  
  405.      default: printf("def ");
  406.           break;
  407.  
  408.      case 4:  printf("four ");
  409.      case 2:  printf("even ");
  410.           break;
  411.      }
  412.  
  413.    value=3;
  414.  
  415. lopp:
  416.    printf("%d",value--);
  417.    if(value) goto lopp;
  418.    printf("one " "two " "three");
  419.  
  420.    for(value=5; value>0; value--)
  421.       {
  422.       if(value%2==0)
  423.          continue;
  424.       printf("%d",value);
  425.       }
  426.  
  427.    value=1; putchar('\n');
  428.  
  429.    while(value<100)
  430.       {
  431.       value=value+1;
  432.       test=2;
  433.       flag=0;
  434.  
  435.       while(test<value)
  436.      { if(value%test==0) flag=1; test=test+1; }
  437.       if(flag==0) { printnum(value); putchar(32); }
  438.       }
  439.    putchar('\n');
  440.  
  441.    printf("\"\'");
  442.    printf("%d %d %d ",mama,Mama,Ma_ma);
  443.    printf("%lx %lx %ld %ld %ld ",wopa,woper,wopra,wopa,woper);
  444.    printf("%g %g %g %g\n",doud,pipi,duba,dubb);
  445.  
  446.    ddza=1.2345;
  447.    ddzb=1.1111;
  448.  
  449.    ab.b=1.2345;
  450.  
  451.    printf("%d%d%d%d%d%d ",ddza!=ddzb,ddza==ddzb,ddza<ddzb,
  452.             ddza>ddzb,ddza<=ddzb,ddza>=ddzb);
  453.  
  454.    printf("%g %g ",ddza+ddzb,ddza-ddzb);
  455.    printf("%g %g ",ddza*ddzb,ddza/ddzb);
  456.    mrakx=L'\377'; mraky='ab'; printf("%d %d ",mrakx,mraky);
  457.  
  458.    printf(L"%g  ",ab.b);
  459.    printf("%4x %4x ",ab.lx.l1,ab.lx.l2);
  460.    printf("%4x %4x\n",ab.lx.l3,ab.lx.l4);
  461. }
  462.  
  463.  
  464. void printnum(int n)
  465. {
  466.    static char *ptr;
  467.  
  468.    ptr=malloc(10); ptr=ptr+9; *ptr=0;
  469.  
  470.    if(n==0) { putchar('0'); return; }
  471.    if(n<0)  { putchar('-'); n= -n;  }
  472.  
  473.    while(n>0)
  474.       {
  475.       *--ptr=(n%10)+'0';
  476.       n=n/10;
  477.       }
  478.  
  479.    while(*ptr) putchar(*ptr++);
  480. }
  481.  
  482. void subrun(int p1, int p2)
  483. {
  484.    int i=4;
  485.    printf("sr %d,%d ",p1,p2);
  486.    while(--i) { if(i==2) continue; printf("%d",i); } i=4;
  487.    do { if(i==2) continue; printf("%d",i); } while(--i);
  488. }
  489.  
  490. int subtwo(int p1, int p2)
  491. {
  492.    printf("s2 %d,%d ",p1,p2);
  493.  
  494.    {
  495.    int atint;
  496.    static int stint=6;
  497.  
  498.    printf("*%d",stint);
  499.    stint=7;
  500.    }
  501.  
  502.    return p2;
  503. }
  504.  
  505. int subbi()
  506. {
  507.    return 423;
  508. }
  509.  
  510. void arsub()
  511. {
  512.    char f[12][11];
  513.    static int arint;
  514.    int i,j, b[5][7];
  515.  
  516.    arint=3;
  517.    goto arlab;
  518.    printf("disaster!!\n");
  519.  
  520. arlab:
  521.    for(i=0; i<5; i++)
  522.       for(j=0; j<7; j++)
  523.      b[i][j]=i+j;
  524.  
  525.    for(i=0; i<5; i++)
  526.       for(j=0; j<7; j++)
  527.      printf("%d ",b[i][j]);
  528.  
  529.    f[3][4]='a'; printf(" %c\n",f[3][4]);
  530. }
  531.  
  532. long lfun()
  533. {
  534.    long lvar;
  535.  
  536.    lvar=(long)54;
  537.    return lvar;
  538. }
  539.  
  540. void filefunc()
  541. {
  542.    char buf[20];
  543.    int fd;
  544.  
  545.    strcpy(buf,"something");
  546.    printf("fd=%d ",fd=create("tfile")); if(fd==-1) exit(0);
  547.    printf("w=%d  ",write(fd,buf,9));
  548.    printf("cc=%d ",close(fd));
  549.  
  550.    strcpy(buf,"any thing");
  551.    printf("o=%d ",fd=open("tfile",0));
  552.    read(fd,buf,20);
  553.    printf("cl=%d %s ",close(fd),buf);
  554. }
  555.  
  556.  
  557. void scopefun()
  558. {
  559.    int out1, out2, out3=2;
  560.  
  561.    out1=3; out2=4;
  562.  
  563.       {
  564.       static int out3=7;
  565.       int out4, out1;
  566.  
  567.       out1=4;
  568.  
  569.      {
  570.      int out1,out2;
  571.  
  572.      out1=5; out2=2;
  573.      printf("%d %d %d ",out2,out1,out3);
  574.      }
  575.  
  576.       printf("%d ",out1,out2);
  577.       }
  578.  
  579.    printf("%d %d %d\n",out1,out2,out3);
  580. }
  581.